import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe, VersioningType } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { WINSTON_LOGGER_TOKEN } from './winston/winston.module';
import * as session from 'express-session';
import * as cookieParser from 'cookie-parser'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { I18nValidationExceptionFilter, I18nValidationPipe } from 'nestjs-i18n';


async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    cors: true,
    // logger: ['warn', 'error']
  });

  app.use(session({
    secret: 'abin', // 指定加密的密钥
    resave: false, // resave 为 true 是每次访问都会更新 session，不管有没有修改 session 的内容，而 false 是只有 session 内容变了才会去更新 session。
    saveUninitialized: false // saveUninitalized 设置为 true 是不管是否设置 session，都会初始化一个空的 session 对象。比如你没有登录的时候，也会初始化一个 session 对象，这个设置为 false 就好
  }))
  // app.useGlobalPipes(new ValidationPipe({
  //   transform: true
  // }))
  app.useGlobalPipes(new I18nValidationPipe())
  app.useGlobalFilters(new I18nValidationExceptionFilter({
    detailedErrors: false
  }))

  app.useStaticAssets('public', {
    prefix: '/static'
  })
  app.enableVersioning({
    type: VersioningType.HEADER,
    header:'version'
  })
  // app.useLogger(new MyLogger())
  // app.useLogger(app.get(WINSTON_LOGGER_TOKEN))
  // app.setGlobalPrefix('/api')
  app.use(cookieParser())
  const config = new DocumentBuilder()
  .setTitle('test swagger')
  .setDescription('The Api description')
  .setVersion('1.0')
  .addTag('test')
  .addBasicAuth({
    type: 'http',
    name: 'basic',
    description: '用户名 + 密码'
  })
  .addCookieAuth('sid',{
    type: 'apiKey',
    name: 'cookie',
    description: '基于 cookie的认证'
  })
  .addBearerAuth({
    type: 'http',
    description: '基于jwt的认证',
    name: 'bearer'
  })
  .build()
  const document = SwaggerModule.createDocument(app, config)
  SwaggerModule.setup('doc', app, document)
  await app.listen(3002);
}
bootstrap();
